package Tools;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.fs.zise.GameSurfaceView;
import com.fs.zise.ZiSeActivity;


import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class AnimationData {
	Bitmap image = null; // sprite image
	boolean tileMode = false;
	Bitmap[] tiles = null;
	short tileCount; // tile count
	short[][] tileData; // tile data
	short frameCount; // frame count
	short[][] frameData; // frame data
	byte actionCount; // action count
	short[][] actionData; // action data

	public static final int SPX_HEADER = 1397772888; // DFSPX

	// sprite class version
	public static final byte SPX_VERSION = 34; // 3.4
	//
	// // byte sequence
	public static final byte SPX_BYTE_SEQUENCE_JAVA = 1;

	public AnimationData(String spxName, Bitmap image) {
		loadSpx(spxName, image);
	}

	public AnimationData(String spxName, Bitmap[] tiles) {
		loadSpx(spxName, image);
		this.tiles = tiles;
		this.tileMode = true;
	}
///**
// * ���������������������Animation ԭ�� �Լ���� ��᲻�� ��Ҫд����
// * @param spxName
// * @param imageName
// */
//	public AnimationData(String spxName, String imageName) {
//		loadSpx(spxName, null);
//
//		if (tileMode) {
//			String name = null;
//			String ext = null;
//			char ch;
//			int namelen = imageName.length();
//			for (int i = namelen - 1; i >= 0; --i) {
//				ch = imageName.charAt(i);
//				if (ch == '.') {
//					ext = imageName.substring(i);
//					name = imageName.substring(0, i);
//					System.out.println("name:"+name);
//					break;
//				}
//			}
//			if (ext == null) {
//				name = imageName;
//				ext = "";
//			}
//
//			tiles = new Bitmap[tileCount];
//
//			String fullName;
//			for (int i = 0; i < tileCount; i++) {
//				fullName = name + i + ext;
//
//				Bitmap spximg = null;
//					System.out.println("load spx image\n");
//					InputStream is;
//					try {
//						is = myViewActivity.assetsAssetManager.open(fullName);
//						spximg = BitmapFactory.decodeStream(is);
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
////					spximg = UtilsBitmap.creatBitmap(fullName);
//				tiles[i] = spximg;
//			}
//		} else {
//			Bitmap spximg = null;
////			spximg = UtilsBitmap.creatBitmap(imageName);
//			InputStream is;
//			try {
//				is = myViewActivity.assetsAssetManager.open(imageName);
//				spximg = BitmapFactory.decodeStream(is);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
////			spximg = UtilsBitmap.creatBitmap(fullName);
//			setImage(spximg);
//		}
//	}
	/**
	 * SPX�ļ���Ҫ����assets��
	 * @param spxName
	 * @param image
	 */
	public void loadSpx(String spxName, Bitmap image) {
		try {
//			InputStream is = UtilsBitmap.loadResource(spxName);  
			AssetManager assetManager = ZiSeActivity.getActivity().getResources().getAssets();
			InputStream is = assetManager.open(spxName);
			DataInputStream data = new DataInputStream(is);
			int header;
			byte sequenceLength;
			byte collisionCount;
			byte referencePointCount;
			short i, j, offset, length;
			byte delay;
			byte frameTileCount;
			byte version;
			byte byteSequence;

			// check spx format
			header = data.readInt();
			if (header != SPX_HEADER) {
				throw new Exception("invalid SpriteX format\n");
			} else {
				version = data.readByte();
				// sprite file and sprite class no matching
				if (version != SPX_VERSION) {
					throw new Exception("version no matching\n");
				}

				// byte sequence check
				byteSequence = data.readByte();
				if ((byteSequence & 1) != SPX_BYTE_SEQUENCE_JAVA) {
					throw new Exception("byte sequence error\n");
				}

				// add
				if ((byteSequence & 2) != 0) {
					tileMode = true;
				}
				System.out.println("timlMode:"+tileMode);
				// read tile
				tileCount = (short)data.readInt();
				// System.out.println("tileCount:" + tileCount);
				if (tileMode == false) {
					tileData = new short[tileCount][4];
					// struct
					// ...
					// array[0] tile x
					// array[1] tile y
					// array[2] tile width
					// array[3] tile height
					// ...
					for (i = 0; i < tileCount; i++) {
						tileData[i][0] = data.readShort();
						tileData[i][1] = data.readShort();
						tileData[i][2] = data.readShort();
						tileData[i][3] = data.readShort();
					}
				}

				// read frame
				frameCount = (short)data.readInt();
				// System.out.println("frameCount:" + frameCount);
				frameData = new short[frameCount][];
				for (i = 0; i < frameCount; i++) {
					frameTileCount = (byte)data.readInt();
					// System.out.println("frameTileCount:" + frameTileCount);
					collisionCount = (byte)data.readInt();
					referencePointCount = (byte) data.readInt();
					length =  (short)(Animation.FRAME_HEADER_SIZE + frameTileCount * 4
							+ collisionCount * 4 + referencePointCount * 2);
					frameData[i] = new short[length];
					// struct
					// array[0] data length
					// array[1] tile count
					// arrat[2] collision count
					// arrat[3] reference count
					// arrat[4] frame width
					// arrat[5] frame height
					// ...
					// array[6] tile index
					// array[7] tile x
					// array[8] tile y
					// array[9] tile transform
					// ...
					// array[10] collision x
					// array[11] collision y
					// array[12] collision width
					// array[13] collision height
					// ...
					// array[14] reference point x
					// array[15] reference point y
					// ...

					frameData[i][0] = (short)length;
					frameData[i][Animation.FRAME_TILE_COUNT_BIT] = frameTileCount;
					frameData[i][Animation.FRAME_COLLISION_COUNT_BIT] = collisionCount;
					frameData[i][Animation.FRAME_REFERENCE_POINT_COUNT_BIT] = referencePointCount;
					frameData[i][Animation.FRAME_TOP_POS_BIT] = data.readShort(); // frame
					// top
					// pos
					frameData[i][Animation.FRAME_BOTTOM_POS_BIT] = data.readShort(); // frame
					// bottom
					// pos
					frameData[i][Animation.FRAME_LEFT_POS_BIT] = data.readShort(); // frame
					// left
					// pos
					frameData[i][Animation.FRAME_RIGHT_POS_BIT] = data.readShort(); // frame
					// right
					// pos

					offset = Animation.FRAME_HEADER_SIZE;

					// read frame tile
					for (j = 0; j < frameTileCount; j++) {
						frameData[i][0 + offset] = data.readShort(); // tile
						// index
						frameData[i][1 + offset] = data.readShort(); // tile
						// x
						frameData[i][2 + offset] = data.readShort(); // tile
						// y
						frameData[i][3 + offset] = data.readShort(); // tile
						// transform
						offset += 4;
					}

					// read collision
					for (j = 0; j < collisionCount; j++) {
						frameData[i][0 + offset] = data.readShort(); // collision
						// x
						frameData[i][1 + offset] = data.readShort(); // collision
						// y
						frameData[i][2 + offset] = data.readShort(); // collision
						// width
						frameData[i][3 + offset] = data.readShort(); // collision
						// height
						offset += 4;
					}

					// reference point
					for (j = 0; j < referencePointCount; j++) {
						frameData[i][0 + offset] = data.readShort(); // reference
						// point
						// x
						frameData[i][1 + offset] = data.readShort(); // reference
						// point
						// y
						offset += 2;
					}
				}

				// read action
				actionCount = (byte) data.readInt();
				// System.out.println("actionCount:" + actionCount);
				// tileCount = data.readInt();
				// System.out.println("tileCount:"+tileCount);
				// if (tileMode == false) {
				// tileData = new int[tileCount][4];
				// //struct
				// //...
				// //array[0] tile x
				// //array[1] tile y
				// //array[2] tile width
				// //array[3] tile height
				// //...
				// for (i = 0; i < tileCount; i++) {
				// System.out.println("i:"+i);
				// tileData[i][0] = data.readShort();
				// tileData[i][1] = data.readShort();
				// tileData[i][2] = data.readShort();
				// tileData[i][3] = data.readShort();
				// }
				// }
				//
				// //read frame
				// frameCount = data.readShort();
				// System.out.println("frameCount:"+frameCount);
				actionData = new short[actionCount][];

				for (i = 0; i < actionCount; i++) {
					sequenceLength = (byte) data.readInt();
					
					 System.out.println("sequenceLength:" + sequenceLength);
					delay = data.readByte();
					 System.out.println("delay:" + delay);
					if (delay == 1) {
						length = (short) (Animation.ACTION_HEADER_SIZE + sequenceLength << 1);
					} else {
						length = (short) (Animation.ACTION_HEADER_SIZE + sequenceLength);
					}
					actionData[i] = new short[length];
					// struct
					// array[0] data length
					// array[1] sequence count
					// array[2] action delay
					// array[3] action transform
					// ...
					// array[4] action sequences
					// array[5] delay time
					// ...
					actionData[i][0] = (short)length; // data length for c copy array
					actionData[i][Animation.ACTION_SEQUENCE_LENGTH_BIT] = sequenceLength; // sequence
					// count
					actionData[i][Animation.ACTION_SEQUENCE_DELAY_BIT] = delay; // action
					// delay
					actionData[i][Animation.ACTION_TRANSFORM_BIT] = (short) data
					.readInt(); // action transform
					// System.out.println("actionData[i][ACTION_TRANSFORM_BIT]:"
					// + actionData[i][ACTION_TRANSFORM_BIT]);
					offset = Animation.ACTION_HEADER_SIZE;
					if (delay == 1) {
						for (j = 0; j < sequenceLength; j++) {
							// read sequence
							actionData[i][offset] = data.readShort();
							// read delay time
							actionData[i][offset + 1] = data.readShort();
							offset += 2;
						}
					} else {
						for (j = 0; j < sequenceLength; j++) {
							// read sequence
							actionData[i][offset] = data.readShort();
							// System.out.println("actionData[i][offset]:"
							// + actionData[i][offset]);
							offset += 1;
						}
					}
				}
			}
			// set image
			setImage(image);

			// load complete
//			visible = true;
			// data.close();
			is.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	

	private void setImage(Bitmap image) {
		this.image = image;
		
//		LlkActivity.getActivity().mGameSurfaceView.getImageFromAssetFile(fileName)
	}
}
